package com.example.naverbooksearch;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

	EditText urlView;
	Handler mHandler = new Handler();
	ListView listView;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        urlView = (EditText) findViewById(R.id.urlText);
        listView = (ListView) findViewById(R.id.listView);
        Button btn = (Button) findViewById(R.id.goBtn);
        btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String urlString = urlView.getText().toString();
				if(urlString != null && !urlString.equals("")) {
					NaverBookRequest request = new NaverBookRequest(urlString);
					NetworkModel.getInstance().getNetworkData(
							request,
							new NetworkRequest.OnProcessCompleteListener() {
								
								@Override
								public void onError(NetworkRequest request, String errorMessage) {
									// TODO Auto-generated method stub
									
								}
								
								@Override
								public void onCompleted(NetworkRequest request) {
									// TODO Auto-generated method stub

									NaverBookRequest rq = (NaverBookRequest) request;
									NaverBooks naverBooks = rq.getResult();
									NaverBookItemAdapter mAdapter = new NaverBookItemAdapter(MainActivity.this, naverBooks.items);
									listView.setAdapter(mAdapter);
									//not implemented...
									
								}
							},
							mHandler);
				}
				
				
			}
		});
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
